/* RPCTranslator.hh -- Tradutor das mensagens recebidas e enviadas.
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 *
 * This file is part of GEARMedula.
 * 
 * GEARMedula is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, version 3 of the License.
 * 
 * GEARMedula is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#ifndef RPCTRANSLATOR_HH_
#define RPCTRANSLATOR_HH_

#include <rpc/xdr.h>

#include <vector>

#include "MessagesConstants.hh"
#include "FieldsConstants.hh"

class FieldInterface;
class MessageInterface;

enum RPCSide
{
    RPCSIDE_CLIENT,
    RPCSIDE_SERVER,
    RPCSIDE_TOTAL
};

class RPCTranslator
{
private:
    FieldInterface * field_table_decode_[ FIELDTYPE_TOTAL ];
    FieldInterface * field_table_encode_[ FIELDTYPE_TOTAL ];
    MessageInterface * message_table_[ MESSAGETYPE_TOTAL ];

    XDR xdrs_incoming_;
    XDR xdrs_outgoing_;
    char * incoming_buffer_;
    char * outgoing_buffer_;

    int ResetSentMessage();
    int ResetReceivedMessage();
    void PopulateFieldTableDecode();
    void PopulateFieldTableEncode();
    void PopulateMessageTable();
    int getMessageNumberOfParametersToSend( MessageType message_type );
    void setMessageType( MessageType message_type );
public:
    RPCTranslator(void);
    void Init( char * incoming_buffer, char * outgoing_buffer );
    int Receive( MessageType *message_type, std::vector< struct FieldContainer> &arguments_received, RPCSide rpc_side );
    int Send( const MessageType message_type, std::vector< struct FieldContainer> arguments_sent, RPCSide rpc_side );
};

#endif  // RPCTRANSLATOR_HH_
